package view;

import controller.RegisterController;
import controller.exception.RegistrationException;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.inject.Named;
import javax.validation.constraints.Size;
import model.Person;
import model.User;
import utils.Hash;
import utils.JSFUtils;

/**
 *
 * @author ZimmE
 */
@Named
@RequestScoped
public class RegisterView {

    @EJB
    private RegisterController registerController;
    private Person person;
    private User user;
    @Size(min = 6, max = 45)
    private String password;

    public RegisterView() {
        this.person = new Person();
        this.user = new User();
        this.user.setPersonId(person);
    }

    /**
     * Get the value of user
     *
     * @return the value of user
     */
    public User getUser() {
        return user;
    }

    /**
     * Set the value of user
     *
     * @param user new value of user
     */
    public void setUser(User user) {
        this.user = user;
    }

    /**
     * Get the value of person
     *
     * @return the value of person
     */
    public Person getPerson() {
        return person;
    }

    /**
     * Set the value of person
     *
     * @param person new value of person
     */
    public void setPerson(Person person) {
        this.person = person;
    }

    /**
     * Get the value of password
     *
     * @return the value of password
     */
    public String getPassword() {
        return password;
    }

    /**
     * Set the value of password
     *
     * @param password new value of password
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * Delegate method for register
     */
    public void register() {
        user.setPassword(Hash.md5(password));
        try {
            registerController.register(user);
            JSFUtils.addMessage(FacesMessage.SEVERITY_INFO, JSFUtils.getLocalizedMessage("message_registration_success"));
        } catch (RegistrationException ex) {
            JSFUtils.addMessage(FacesMessage.SEVERITY_ERROR, JSFUtils.getLocalizedMessage("message_registratoin_failed"));
        }

    }
}
